extends Node2D
@onready var test_2: Node2D = $"."
@onready var animatable_body_2d: AnimatableBody2D = $AnimatableBody2D
@onready var collision_shape_2d: CollisionShape2D = $AnimatableBody2D/CollisionShape2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatableBody2D/AnimatedSprite2D
@onready var label: Label = $Label
@onready var label_1: Label = $AnimatableBody2D/Label
var my_position: int = 0
var my_image: Image
var click_position: int = 0
@export var image_name: String = "attack_1"
signal click_npc(info: Vector2, size: Vector2, position: int)
signal my_ttttt(position: int)
var speed: float = 60.0 # 像素/秒
var initial_position = 0
var jieshu_position = 0
func _ready() -> void:
	initial_position = label_1.position.y
	jieshu_position = label_1.position.y - 60

func _process(delta: float) -> void:
	if label_1.visible:
		label_1.position.y -= speed * delta
		if label_1.position.y <= jieshu_position:
			label_1.position.y = initial_position
			label_1.visible = false

func _on_animatable_body_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	var target_position = get_local_mouse_position()
	var sss = animatable_body_2d.position - Vector2(my_image.get_width() / 2, my_image.get_height() / 2)
	var aaa = target_position - sss
	var bbb = my_image.get_pixelv(aaa)
	if bbb.r != 0 || bbb.g != 0 || bbb.b != 0:
		if event is InputEventMouseButton:
			if event.is_pressed() and event.button_index == MouseButton.MOUSE_BUTTON_LEFT:
				emit_signal("click_npc", test_2.position, Vector2(my_image.get_width(), my_image.get_height()), click_position)

func _on_animated_sprite_2d_animation_looped() -> void:
	my_position = 0
	
func _on_animated_sprite_2d_frame_changed() -> void:
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, my_position).get_image()
	my_position += 1

func set_stand(path: String, name: String, role_name: String, position: int) -> void:
	click_position = position
	my_position = 0
	image_name = name
	var sprite_frames = load(path)
	animated_sprite_2d.sprite_frames = sprite_frames
	animated_sprite_2d.sprite_frames.set_animation_loop(image_name, true)
	animated_sprite_2d.play(image_name)
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, 0).get_image()
	var aaa = RectangleShape2D.new()
	aaa.size = Vector2(my_image.get_width(), my_image.get_height())
	collision_shape_2d.shape = aaa
	label.text = role_name
	label.position = Vector2(0 - (label.size.x / 2), my_image.get_height() / 2)
	
func setImage(path: String, name: String, role_name: String, position: int, is_loop: bool) -> void:
	click_position = position
	my_position = 0
	image_name = name
	var sprite_frames = load(path)
	animated_sprite_2d.sprite_frames = sprite_frames
	animated_sprite_2d.sprite_frames.set_animation_loop(image_name, is_loop)
	animated_sprite_2d.play(image_name)
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, 0).get_image()
	var aaa = RectangleShape2D.new()
	aaa.size = Vector2(my_image.get_width(), my_image.get_height())
	collision_shape_2d.shape = aaa
	label.text = role_name
	label.position = Vector2(0 - (label.size.x / 2), my_image.get_height() / 2)

## 设置物理攻击动画
## @param path: String 目标位置
## @param name: String 目标位置
## @param role_name: String 目标位置
## @param position: String 目标位置
func set_physical_attack(path: String, name: String, role_name: String, position: int) -> void:
	click_position = position
	my_position = 0
	image_name = name
	var sprite_frames = load(path)
	animated_sprite_2d.sprite_frames = sprite_frames
	animated_sprite_2d.sprite_frames.set_animation_loop(image_name, false)
	animated_sprite_2d.play(image_name)
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, 0).get_image()
	var aaa = RectangleShape2D.new()
	aaa.size = Vector2(my_image.get_width(), my_image.get_height())
	collision_shape_2d.shape = aaa
	label.text = role_name
	label.position = Vector2(0 - (label.size.x / 2), my_image.get_height() / 2)
	
## 设置遭受攻击动画
## @param path: String 目标位置
## @param name: String 目标位置
## @param role_name: String 目标位置
## @param position: String 目标位置
func set_under_attack(path: String, name: String, role_name: String, position: int, ap: String) -> void:
	click_position = position
	my_position = 0
	image_name = name
	var sprite_frames = load(path)
	animated_sprite_2d.sprite_frames = sprite_frames
	animated_sprite_2d.sprite_frames.set_animation_loop(image_name, false)
	animated_sprite_2d.play(image_name)
	my_image = animated_sprite_2d.sprite_frames.get_frame_texture(image_name, 0).get_image()
	var aaa = RectangleShape2D.new()
	aaa.size = Vector2(my_image.get_width(), my_image.get_height())
	collision_shape_2d.shape = aaa
	label.text = role_name
	label.position = Vector2(0 - (label.size.x / 2), my_image.get_height() / 2)
	label_1.text = ap
	label_1.visible = true

func _on_animated_sprite_2d_animation_finished() -> void:
	emit_signal("my_ttttt", click_position)
